﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.Skins;
using DevExpress.LookAndFeel;
using DevExpress.UserSkins;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraBars.Helpers;
using CNotebook;
using CNotebook.Inventory;

namespace NotebookS2
{
    public partial class M_BRND_CT_PRD : RibbonForm
    {
        public M_BRND_CT_PRD()
        {
            InitializeComponent();
        }

        public string ConnectionString { get; set; }

        void BindBrand(string ItemGroup)
        {
            BrandManager oManager = new BrandManager();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();
            DataTable _table = oManager.GetBrands(ItemGroup);
            oManager.Close();

            cboBrand.DisplayMember = "BrandName";
            cboBrand.ValueMember = "BrandID";
            cboBrand.DataSource = _table;
            cboBrand.Refresh();

        }

        void BindCategory(string _BrandID)
        {
            BrandCategoryManager oManager = new BrandCategoryManager();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();
            DataTable _table = oManager.GetCategoryByBrandID(_BrandID);
            oManager.Close();

            cboCategory.DisplayMember = "CatgryName";
            cboCategory.ValueMember = "CatgryCode";
            cboCategory.DataSource = _table;
            cboCategory.Refresh();

        }

        private void M_BRND_CT_PRD_Load(object sender, EventArgs e)
        {

        }

        private void cboItemGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindBrand(cboItemGroup.Text);
        }

        private void cboBrand_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindCategory(cboBrand.SelectedValue.ToString());
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (btnAdd.Text == "Add")
            {
                ProductManager oManager = new ProductManager();

                oManager.ConnectionString = this.ConnectionString;
                oManager.Open();

                oManager.AddProduct(cboBrand.SelectedValue.ToString(), cboCategory.SelectedValue.ToString(), txtProdCode.Text, txtProdName.Text);

                oManager.Close();

                txtProdCode.Text = "";
                txtProdName.Text = "";
                MessageBox.Show("Successfully added", "Product", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                ProductManager oManager = new ProductManager();

                oManager.ConnectionString = this.ConnectionString;
                oManager.Open();

                oManager.UpdateProduct(cboBrand.SelectedValue.ToString(), cboCategory.SelectedValue.ToString(), txtProdCode.Text, txtProdName.Text);

                oManager.Close();

                MessageBox.Show("Successfully updated", "Product", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void txtProdCode_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            FND_BRND_CT_PRD oForm = new FND_BRND_CT_PRD();

            oForm.ConnectionString = this.ConnectionString;
            oForm.CategoryID = cboCategory.SelectedValue.ToString();
            oForm.CategoryName = cboCategory.Text;
            oForm.BrandID = cboBrand.SelectedValue.ToString();

            oForm.ShowDialog();

            if (oForm.ProdCode != null)
            {
                txtProdCode.Text = oForm.ProdCode;
                txtProdName.Text = oForm.ProdName;

                btnAdd.Text = "Update";

                txtProdCode.Properties.ReadOnly = true;
            }
        }

        private void cboCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtProdCode.Text = "";
            txtProdName.Text = "";
            txtProdCode.Properties.ReadOnly = false;
            btnAdd.Text = "Add";
        }
    }
}
